package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.service.ShoppingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 创建数据库 entity 实体对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 添加用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 查询自己的购物车数据是否存在当前商品
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(wrapper);
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            // 购物车已经存在当前商品
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart);
        } else {
            // 购物车不存在当前商品
            if (shoppingCart.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                // 添加商品名称
                shoppingCart.setName(dish.getName());
                // 添加 image
                shoppingCart.setImage(dish.getImage());
                // 添加金额
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Setmeal setmeal = setmealMapper.getBySetmealId(shoppingCart.getSetmealId());
                // 添加商品名称
                shoppingCart.setName(setmeal.getName());
                // 添加 image
                shoppingCart.setImage(setmeal.getImage());
                // 添加金额
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 添加创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 添加数量
            shoppingCart.setNumber(1);
            // 向购物车插入数据
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> list(Long userId) {
        if (userId == null) {
            throw new BaseException(MessageConstant.USER_NOT_LOGIN);
        }

        return shoppingCartMapper.selectList(new QueryWrapper<ShoppingCart>().lambda().
                eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
    }


    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 查询该商品
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor());
        List<ShoppingCart> list = shoppingCartMapper.selectList(wrapper);

        if (list == null && list.size() <= 0) {
            throw new BaseException(MessageConstant.DISH_NOT_FOUND);
        }
        // 判断购物车该商品的数量
        shoppingCart = list.get(0);
        if (shoppingCart.getNumber() == 1) {
            // 如果只有一份就删除该商品
            shoppingCartMapper.deleteById(shoppingCart.getId());
        } else {
            // 如果不止一份则更新商品数量
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateById(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.delete(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
    }
}


